﻿using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using System.Threading.Tasks;
using System.Collections.Generic;
using WMSAML.SY;
using WMSADBUtility;

namespace WMSADL.SY
{
    public class TSystemRegistDAL
    {
        /// <summary>
        /// 
        /// </summary>
        public int Add_SystemRegist(TSystemRegistEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TSystemRegist(");
            strSql.Append("PcCode,RegCode,Mark)");

            strSql.Append(" values (");
            strSql.Append("@PcCode,@RegCode,@Mark)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PcCode", DbType.String, entity.PcCode);
            db.AddInParameter(dbCommand, "RegCode", DbType.String, entity.RegCode);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        ///  
        /// </summary>
        /// <param name="pcCode">机器码</param>
        /// <returns>bool</returns>
        public bool Validate_SystemRegist(string pcCode)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("SELECT COUNT(*) FROM TSystemRegist WHERE PcCode=@PcCode ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL.ToString()))
            {
                db.AddInParameter(dbCommand, "PcCode", DbType.String, pcCode);
                bool falg = false;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, 0)) || (Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    falg = false;
                }
                else
                {
                    falg = true;
                }
                return falg;
            }
        }

        /// <param name="pcCode">机器码</param>
        /// <returns>bool</returns>
        public bool Validate_SystemRegist()
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("SELECT COUNT(*) FROM TSystemRegist ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL.ToString()))
            {
                bool falg = false;
                object obj = db.ExecuteScalar(dbCommand);
                if ((Object.Equals(obj, 0)) || (Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    falg = false;
                }
                else
                {
                    falg = true;
                }
                return falg;
            }
        }

        /// <summary>
        /// 获取记录详细信息
        /// </summary>
        /// <returns>SystemRegist</returns>
        public TSystemRegistEntity GetEntity_SystemRegist(string pcCode)
        {
            string strSQL = "SELECT * FROM TSystemRegist WHERE PcCode=@PcCode";
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSQL))
            {
                db.AddInParameter(dbCommand, "PcCode", DbType.String, pcCode);
                TSystemRegistEntity objInfo = new TSystemRegistEntity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        objInfo.PcCode = dataReader["PcCode"].ToString();
                        objInfo.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        objInfo.RegCode = dataReader["RegCode"].ToString();
                        objInfo.Mark = dataReader["Mark"].ToString();
                    }
                }
                return objInfo;
            }
        }


        /// <summary>
        ///  删除系统注册信息
        /// </summary>
        /// <returns>bool</returns>
        public bool Del_SystemRegist(string pcCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TSystemRegist ");
            strSql.Append(" where PcCode=@PcCode ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PcCode", DbType.String, pcCode);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <param name="connString"></param>
        /// <returns></returns>
        public bool ConnectionSQL()
        {
            bool result = true;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(Database.ConnString);
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    result = true;
                    conn.Close();
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                result = false;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Dispose();
                    conn = null;
                }
            }
            return result;
        }

        /// <summary>
        /// 连接数据库
        /// </summary>
        public bool ConnectionSQL(string connString)
        {
            bool result = true;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(connString);
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    result = true;
                    conn.Close();
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                result = false;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Dispose();
                    conn = null;
                }
            }
            return result;
        }
    }
}
